package com.example.demo.security.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * 登录用户实体类
 * @author zjj
 * @date 2021-05-24
 */
@Getter
@AllArgsConstructor
public class JwtUser implements UserDetails {
    private final String username;
    @JsonIgnore
    private final String password;
    private final Long id;
    private final int sex;
    private final String email;
    private final String phone;
    private final Boolean status;
    private Timestamp createTime;
    @JsonIgnore
    private final Date lastPwdResetDate;
    private final String dept;
    @JsonIgnore
    private final Collection<GrantedAuthority> authorities;
    private final boolean enabled;

    @JsonIgnore
    @Override
    public String getPassword() {
        return password;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return status;
    }

    public Collection getRoles(){
        return authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet());
    }
}
